$('#report_div').load('admin/sales_report/');
$('#week').show();
$('#period').livequery('change', function() {
	var period_by = $(this).val();
	$('.display-none').hide();
	$('#'+period_by).show();
});

$('#period').livequery('change', period_change);
$('.day_value, .week_value, .month_value, .month_year_value').livequery('change', val_change);


$('.update_graph').button({icons:{primary:'ui-icon-refresh'}});
$('.prev_button').button({icons:{primary:'ui-icon-circle-triangle-w'}});
$('.next_button').button({icons:{primary:'ui-icon-circle-triangle-e'}});
$('.day_value, .two_day_value, .week_value').datepicker({ 
	dateFormat: 'yy-mm-dd',
	onClose: function(dateText, inst) {$(this).blur();}
});
$('.update_graph').click(period_change);
//button
var start, end, period, month, year;

$('.prev_button').click(function(){
	period = $('#period').val();
	month = $('.month_value').val();
	year = $('.month_year_value').val();
	if(period == 'day')
	{
		start = format(Date.parse($('.day_value').val()).add(-1).days());
		end = start;
		set_day(start);
	}
	else if(period =='week')
	{
		var day_number =  Date.parse($('.week_value').val()).getDay();
		start = format(Date.parse($('.week_value').val()).add(-7-day_number).days());
		end = format(Date.parse(start).add(6).days());
		set_week(format(Date.parse($('.week_value').val()).add(-7).days()),start,end);
	}
	else if(period == 'month')
	{
		start = format(Date.parse(year+'-'+month+'-01').add(-1).month());
		end = format(Date.parse(start).add(1).month().add(-1).days());
		var month_year = start.split('-');
		set_month(month_year[1],month_year[0]);
	}
	$('#report_div').load('admin/sales_report/'+period+'/'+start+'/'+end);
});

$('.next_button').click(function(){
	period = $('#period').val();
	month = $('.month_value').val();
	year = $('.month_year_value').val();
	if(period == 'day')
	{
		start = format(Date.parse($('.day_value').val()).add(1).days());
		end = start;
		set_day(start);
	}
	else if(period =='week')
	{
		var day_number =  Date.parse($('.week_value').val()).getDay();
		start = format(Date.parse($('.week_value').val()).add(7-day_number).days());
		end = format(Date.parse(start).add(6).days());
		set_week(format(Date.parse($('.week_value').val()).add(7).days()),start,end);
	}
	else if(period == 'month')
	{
		start = format(Date.parse(year+'-'+month+'-01').add(+1).month());
		end = format(Date.parse(start).add(1).month().add(-1).days());
		var month_year = start.split('-');
		set_month(month_year[1],month_year[0]);
	}
	$('#report_div').load('admin/sales_report/'+period+'/'+start+'/'+end);
});
//end button


function period_change()
{
	var general_date = $('#general_date').val();
	period = $('#period').val();
	if(period == 'day')
	{
		start = general_date;
		end = general_date;
		set_day(general_date);
	}
	else if(period =='week')
	{
		var day_number =  Date.parse(general_date).getDay();
		start = format(Date.parse(general_date).add(-day_number).days());
		end = format(Date.parse(start).add(6).days());
		set_week(format(Date.parse(general_date)),start,end);
	}
	else if(period == 'month')
	{
		var _date = general_date.split('-');
		start = _date[0]+'-'+_date[1]+'-01';
		end = format(Date.parse(start).add(1).month().add(-1).days());
		set_month(_date[1],_date[0]);
	}
	$('#report_div').load('admin/sales_report/'+period+'/'+start+'/'+end);
}

function val_change()
{
	period = $('#period').val();
	month = $('.month_value').val();
	year = $('.month_year_value').val();
	var period_val = $(this).val();
	if(period == 'day')
	{
		start = period_val;
		end = start;
		set_day(start);
	}
	else if(period =='week')
	{
		var day_number =  Date.parse(period_val).getDay();
		start = format(Date.parse(period_val).add(-day_number).days());
		end = format(Date.parse(start).add(6).days());
		set_week(format(Date.parse(period_val)),start,end);
	}
	else if(period == 'month')
	{
		start = format(Date.parse(year+'-'+month+'-01'));
		end = format(Date.parse(start).add(1).month().add(-1).days());
		var month_year = start.split('-');
		set_month(month_year[1],month_year[0]);
	}
	$('#report_div').load('admin/sales_report/'+period+'/'+start+'/'+end);
}

function format(date)
{
	var new_date = date.format('yyyy-mm-dd');
	return new_date;
}

function set_day(d)
{
	$('.day_value , #general_date').val(d);
}

function set_week(d,s,e)
{
	$('.week_value, #general_date').val(d);
	$('.week_start_value').val(s);
	$('.week_end_value').val(e);
}

function set_month(m,y)
{
	$('#general_date').val(y+'-'+m+'-01');
	$('.month_value').val(m);
	$('.month_value_year').val(y);
}